﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UserInterfaceLayer.User
{
    public partial class Request : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblFailure.Visible = false;
            lblInvalidInput.Visible = false;
            lblSuccessful.Visible = false;
            lblNewItem.Visible = false;
            lblDeletedItem.Visible = false;
            btnDelete.Visible = false;
            divDetails.Visible = false;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            string strRequestID = Request.QueryString["RequestID"];
            DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
            FillDropDowns(db);
            if (strRequestID != "add")
            {
                int RequestID;
                if (!int.TryParse(strRequestID, out RequestID))
                    Response.Redirect("~/");
                DataAccessLayer.Request mRequest = DataAccessLayer.Request.FindByRequestID_IncludeDeleted(db, RequestID);
                if (mRequest == null)
                    Response.Redirect("~/");
                FromDatabase(mRequest);
                btnDelete.Visible = true;
                divDetails.Visible = true;
                lvHistory.DataSource = BusinessLogicLayer.DataObject.Request.Convert(DataAccessLayer.Request.HistoryToListByRequestID(db, RequestID));
                lvHistory.DataBind();

                lvRequestRoom.DataSource = BusinessLogicLayer.DataObject.RequestRoom.Convert(DataAccessLayer.RequestRoom.ToListByRequestID_IncludeDeleted(db, RequestID));
                lvRequestRoom.DataBind();
                lvRequestPerson.DataSource = BusinessLogicLayer.DataObject.RequestPerson.Convert(DataAccessLayer.RequestPerson.ToListByRequestID_IncludeDeleted(db, RequestID));
                lvRequestPerson.DataBind();
                lvMessage.DataSource = BusinessLogicLayer.DataObject.Message.Convert(DataAccessLayer.Message.ToListByRequestID_IncludeDeleted(db, RequestID));
                lvMessage.DataBind();
                lvCost.DataSource = BusinessLogicLayer.DataObject.Cost.Convert(DataAccessLayer.Cost.ToListByRequestID_IncludeDeleted(db, RequestID));
                lvCost.DataBind();
                lvCar.DataSource = BusinessLogicLayer.DataObject.Car.Convert(DataAccessLayer.Car.ToListByRequestID_IncludeDeleted(db, RequestID));
                lvCar.DataBind();
                lvSafekeeping.DataSource = BusinessLogicLayer.DataObject.Safekeeping.Convert(DataAccessLayer.Safekeeping.ToListByRequestID_IncludeDeleted(db, RequestID));
                lvSafekeeping.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strRequestID = Request.QueryString["RequestID"];
                if (strRequestID != "add")
                {
                    int RequestID = int.Parse(strRequestID);
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    //DataAccessLayer.Request Request = DataAccessLayer.Request.FindByRequestID_IncludeDeleted(db, RequestID);
                    DataAccessLayer.Request NewRequest = new DataAccessLayer.Request();
                    NewRequest.RequestID = RequestID;
                    NewRequest.EditorUserID = UserID;
                    NewRequest.Timestamp = DateTime.Now;
                    ToDatabase(NewRequest);
                    db.Requests.InsertOnSubmit(NewRequest);
                    db.SubmitChanges();
                    lblSuccessful.Visible = true;
                }
                else
                {
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    DataAccessLayer.sRequest sRequest = new DataAccessLayer.sRequest();
                    db.sRequests.InsertOnSubmit(sRequest);
                    db.SubmitChanges();
                    DataAccessLayer.Request NewRequest = new DataAccessLayer.Request();
                    NewRequest.RequestID = sRequest.ID;
                    NewRequest.EditorUserID = UserID;
                    NewRequest.Timestamp = DateTime.Now;
                    ToDatabase(NewRequest);
                    db.Requests.InsertOnSubmit(NewRequest);
                    db.SubmitChanges();
                    lblNewItem.Visible = true;
                    btnSave.Visible = false;
                }
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strRequestID = Request.QueryString["RequestID"];
                int RequestID = int.Parse(strRequestID);
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                //DataAccessLayer.Request Request = DataAccessLayer.Request.FindByRequestID_IncludeDeleted(db, RequestID);
                DataAccessLayer.Request NewRequest = new DataAccessLayer.Request();
                NewRequest.RequestID = RequestID;
                NewRequest.EditorUserID = UserID;
                NewRequest.Timestamp = DateTime.Now;
                NewRequest.IsDeleted = true;
                ToDatabase(NewRequest);
                db.Requests.InsertOnSubmit(NewRequest);
                db.SubmitChanges();
                lblSuccessful.Visible = true;
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        public void FromDatabase(DataAccessLayer.Request mRequest)
        {
            ddlReferer.SelectedValue = mRequest.RefererID.ToString();
            ddlHeadman.SelectedValue = mRequest.HeadmanPersonID.ToString();
            ddlStatus.SelectedValue = mRequest.RequestStatusID.ToString();
            txtDescription.Text = mRequest.Description;
            txtFeedback.Text = mRequest.Feedback;
            lblDeletedItem.Visible = mRequest.IsDeleted;
        }

        public void ToDatabase(DataAccessLayer.Request mRequest)
        {
            mRequest.RefererID = int.Parse(ddlReferer.SelectedValue);
            mRequest.HeadmanPersonID = int.Parse(ddlHeadman.SelectedValue);
            mRequest.RequestStatusID = int.Parse(ddlStatus.SelectedValue);
            mRequest.Description = txtDescription.Text;
            mRequest.Feedback = txtFeedback.Text;
        }

        public void FillDropDowns(DataAccessLayer.linqDatabaseDataContext db)
        {
            ddlHeadman.DataSource = DataAccessLayer.Person.ToList(db);
            ddlHeadman.DataTextField = "DisplayName";
            ddlHeadman.DataValueField = "PersonID";
            ddlHeadman.DataBind();

            ddlReferer.DataSource = DataAccessLayer.Referer.ToList(db);
            ddlReferer.DataTextField = "DisplayName";
            ddlReferer.DataValueField = "RefererID";
            ddlReferer.DataBind();
        }
    }
}